package com.bd.scm.module.utils.wx;

/**
 * @ClassName WXAuthUtil
 * @Description TODO
 * @Author YH
 * @Date 2019/4/16 9:29
 * @Version 1.0
 **/

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bd.utils.PropertiesUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Properties;

public class WXAuthUtil {

    private static final String RESOURCENAME = "weChatConfig.properties";

    /**
     *
     */
    private static final Properties CONFIG = PropertiesUtil.loadProperties(RESOURCENAME);

    public static Properties getProperties() {
        return CONFIG;
    }

    //开发者ID(AppID)
    public static String APPID = getProperties().getProperty("APPID");
    //开发者密码(AppSecret)
    public static String APPSECRET = getProperties().getProperty("APPSECRET");
    //令牌(Token)
    public static String TOKEN = getProperties().getProperty("TOKEN");
    // 需要加密的明文
    public static String encodingAesKey = getProperties().getProperty("encodingAesKey");
    //这个url的域名必须要进行再公众号中进行注册验证，这个地址是成功后的回调地址
    public static String backUrl = getProperties().getProperty("backUrl");

    //前端地址
    public static String frontPath = getProperties().getProperty("frontPath");

    //访客有效期
    public static Integer validity = Integer.valueOf(getProperties().getProperty("validity"));

    public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException {
        JSONObject jsonObject =null;
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet httpGet =new HttpGet(url);
        HttpResponse response =  client.execute(httpGet);
        HttpEntity entity =response.getEntity();
        if(entity!=null)
        {
            //把返回的结果转换为JSON对象
            String result = EntityUtils.toString(entity, "UTF-8");
            jsonObject = JSON.parseObject(result);
        }

        return jsonObject;
    }

}
